/*
 * @Author: sxb
 * @Email: sxb@mail.com
 * @Date: 2020-02-18 11:17:51
 * @Descripttion: 
 */
import { Component, ElementRef, HostListener, OnInit, ViewChild } from '@angular/core';
import { TabManageService } from './tab-manage.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { HCommonService } from 'src/app/h-common/h-common.service';

@Component({
  selector: 'app-tab-manage',
  templateUrl: './tab-manage.component.html',
  styleUrls: ['./tab-manage.component.scss']
})
export class TabManageComponent implements OnInit {

  txtCode: any = '';
  txtName: any = '';
  bornRoles = {};

  save(){
    let checkedData = [];
    let isExistLabel = true;
    this.listOfData.forEach(item=>{
      if(item.checked){
          if(item.label != ''){
            checkedData.push(item);
          }else{
            this.message.warning("请添加标签内容！");
            isExistLabel = false;
          }
      }
    });
    if(isExistLabel){
      this.putMateriaLabel(checkedData)
    }
  }
  async putMateriaLabel(arrayData){
    if(arrayData.length == 1){
      let data = {
        id: arrayData[0].id,
        materialId: arrayData[0].materialId,
        label: arrayData[0].label
      };
      await this.TabService.putMaterialLabel(data).subscribe(res => {
        this.getAllData()
      })
    }else{
      this.message.warning("请选择一条数据进行提交！");
    }
  }

  //--分页部分
  pageIndex: any = '1';//table当前页
  pageSize: any = '10';//table每页条数
  totalSize: any = '';//总共条数
  listOfData  = [];
  // table页码点击事件
  clickPage(current) {
    this.pageIndex = current
    this.getAllData()
  }
  // 获取右侧人员表格
  async getAllData() {
    let data = {
        code: this.txtCode,
        name: this.txtName,
        page: this.pageIndex - 1 < 0 ? 0 : this.pageIndex - 1,
        size: this.pageSize
    }
    let list: any = await this.TabService.getMaterialLabelList(data).subscribe(res => {
      res.content.forEach(item => {
          item.checked = false;
      })
      this.totalSize = res.totalElements
      this.listOfData = res.content;
    });
  }
  getSearchData(){
    this.pageIndex = 1;
    this.getAllData();
  }
  
  isAllChecked = false;
  // checkAll(value: boolean): void {
  //   this.listOfData.forEach(item => {
  //       item.checked = value;
  //   })
  // }
  // checkSingle() {
  //     let _isAllCheck = true;
  //     this.listOfData.forEach(item => {
  //         if (!item.checked) {
  //             _isAllCheck = false;
  //         }
  //     })
  //     this.isAllChecked = _isAllCheck;
  //   }
  checkSingle(data){
    this.listOfData.forEach(item => {
      if(item.checked){
        item.checked = false;
      }
    })
    //this.selMaterialObj = data;
    data.checked = true;
  }
  
  
  //--弹出框部分  
  txtAltCode: any = '';
  txtAltName: any = '';

  isVisible: Boolean = false;
  isOkLoading: Boolean = false;
  altPageIndex: any = '1';//table当前页
  altPageSize: any = '8';//table每页条数
  altTotalSize: any = '';//总共条数
  altListOfData  = [];
  selMaterialObj = {};
  // table页码点击事件
  altClickPage(current) {
    this.altPageIndex = current
    this.getMaterialData()
  }
  // 获取右侧人员表格
  async getMaterialData() {
    let data = {
        code: this.txtAltCode,
        name: this.txtAltName,
        page: this.altPageIndex - 1 < 0 ? 0 :this.altPageIndex - 1,
        size: this.altPageSize
    }
    let list: any = await this.TabService.getMaterialList(data).subscribe(res => {
      res.content.forEach(item => {
          item.checked = false;
      })
      this.altTotalSize = res.totalElements
      this.altListOfData = res.content;
    });
  }
  getAltSearchData(){
    this.altPageIndex = 1;
    this.getMaterialData();
  }
  showModal(): void {
    this.getMaterialData();
    this.isVisible = true;
  }
  handleOk(): void {
    let checked = false;
    this.altListOfData.forEach(item=>{
      if(item.checked){
        checked = true;
        this.isOkLoading = true;
        this.PostMaterialLabel(item.id);
        this.isVisible = false;
        this.isOkLoading = false;
      }
    });
    if(!checked){
      this.message.warning("请选择物料！");
    }
  }
  handleCancel(): void {
    this.isVisible = false;
  }
  async PostMaterialLabel(id: string) {
    let data = {
      "label": "请完善标签信息",
      "materialId": id
    }
    await this.TabService.postMaterialLabel(data).subscribe(res => {
      this.getAllData()
    })
  }
  altCheckSingle(data) {
    this.altListOfData.forEach(item => {
      if(item.checked){
        item.checked = false;
      }
    })
    this.selMaterialObj = data;
    data.checked = true;
  }

  constructor(
    private message: NzMessageService,
    private TabService: TabManageService,
    private commonService : HCommonService
  ) { }

  ngOnInit() {
    /* 获取权限 */
    this.bornRoles = this.commonService.getRoles("materialLabel");

    this.getAllData()
  }

}
